/**
 * useRadios 当做tab切换 封装
 * @Author: linzy
 * @Date: 2023-01-17
 * @function useRadios
 * @param {string} name - 字段名
 * @param {object} tableForm - 页面表格的tableForm对象
 * @param {tableFormSet} tableForm - 页面表格的tableFormSet方法
 * @param {*} ... - antd radio.group组件的属性
 * @returns [radios, props];
 * @example
  const radiosOptions = [
    { label: '待结算', value: 1 },
    { label: '结算中', value: 2 },
    { label: '待打款', value: 3 },
    { label: '已结算', value: 4 },
  ]
  const [Radios, radiosProps] = useRadios({
    options: radiosOptions,
    name: 'settlementStatus',
    tableForm,
    tableFormSet,
  })
 */
import { Radio } from "antd";
import { setDefaultAttrs } from "@/hooks/tools";

const radios = (props) => {
  const { name, tableForm, tableFormSet, options = [], ...restProps } = props;
  setDefaultAttrs(restProps, {
    buttonStyle: "solid",
    defaultValue: tableForm?.[name] ?? options[0]?.value,
  });
  if (name && tableForm && tableFormSet) {
    setDefaultAttrs(restProps, {
      onChange(e) {
        tableFormSet(state => {
          return { ...state, [name]: e.target.value }
        })
      }
    });
  }
  return (
    <div style={{ margin: '16px 0' }}>
      <Radio.Group {...restProps}>
        {options.map(item => {
          return <Radio.Button key={item.value} {...item}>{item.label}</Radio.Button>
        })}
      </Radio.Group>
    </div>
  );
};

export default (props) => {
  return [radios, props];
};
